<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/** 游戏标签管理 */
class Tag extends AdminController {
	public $_validation = null;
	function __construct(){
		parent::__construct();
		$this->load->model('MTag');
		$this->_validation = Array(
			array('field'=>'code', 'label'=>'目录代码', 'rules'=>'required|max_length[64]|callback__checkCode')
			,array('field'=>'tag', 'label'=>'游戏标签', 'rules'=>'required|max_length[64]|callback__checkTag')
			,array('field'=>'update_time', 'label'=>'更新时间', 'rules'=>'')
		);
	}
	function add(){
		$obj = $this->MTag->createVo();
		$this->load->library('form_validation');
		$this->form_validation->set_rules($this->_validation);
		if($this->form_validation->run()==TRUE){
			$this->load->helper('populate');
			$newObj = populate((Object)((Array)$obj), $this->form_validation->post());
			if($this->MTag->add($newObj)){
				successAndRedirect(l('edit_success'));
			}else{
				errorAndRedirect(l('data_fail'));
			}
		}
		$this->assign('obj', $obj);
		$this->layout();
	}
	
	function edit($id){
		if(!is_numeric($id)){
			show_error(l('id_not_null'));
		}
		$obj = $this->MTag->getOne(Array('id'=>$id));
		if(!$obj){
			errorAndRedirect('未找到定义！');
		}
		$this->editObj = $obj;
		$this->load->library('form_validation');
		$this->form_validation->set_rules($this->_validation);
		if($this->form_validation->run()==TRUE){
			$this->load->helper('populate');
			$newObj = populate((Object)((Array)$obj), $this->form_validation->post());
			if($this->MTag->update($newObj)){
				successAndRedirect(l('edit_success'));
			}else{
				errorAndRedirect(l('data_fail'));
			}
		}
		$this->assign('obj', $obj);
		$this->layout();
	}
	function delete($id, $update_time){
		if(!is_numeric($id) || !is_numeric($update_time)){
			show_error(l('id_not_null'));
		}
		if(!$this->MTag->delete($id, $update_time)){
			errorAndRedirect(l('data_fail'));
		}
		successAndRedirect(l('delete_success'));
	}
	function index($game_tag=null){
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		$this->formfilter->addFilter('code', 'like');
		$this->formfilter->addFilter('tag', 'like');
		$limit = $this->pagination($this->MTag->getCount());
		$list = $this->MTag->getList($limit);
		
		$this->assign('list', $list);
		$this->layout();
	}
	/**
	 * 检查tag是否有重复
	 * @param string $name 标签名称
	 */
	function _checkTag($name){
		$where = Array('tag'=>$name);
		if(isset($this->editObj) && $this->editObj){
			$where['id <>'] = $this->editObj->id;
		}
		if($this->MTag->getAll($where)){
			$this->form_validation->set_message('_checkTag', '已经存在相同名称的标签！');
			return false;
		}
		return true;
	}
	/**
	 * 检查代码是否有重复
	 * @param string $code 标签代码
	 */
	function _checkCode($code){
		$where = Array('code'=>$code);
		if(isset($this->editObj) && $this->editObj){
			$where['id <>'] = $this->editObj->id;
		}
		if($this->MTag->getAll($where)){
			$this->form_validation->set_message('_checkCode', '已经存在相同代码的标签！');
			return false;
		}
		return true;
	}
	
	/**
	 * 读取指定网站下的目录树
	 * @param int $site_id
	 */
	function ajaxGetTree(){
		$categories = $this->MCmsCategory->getAll();
		$t['0'] = array('text'=>'/','id'=>'0','pid'=>NULL);
		foreach ($categories as $c){
			$t[$c->id] = array('text'=>$c->name,'id'=>$c->id,'pid'=>$c->parent_id);
		}
		foreach ($t as $k => $node){
			if( !is_null($node['pid']) ) {
				$t[$node['pid']]['children'][] =& $t[$k];
			}
		}
		echo json_encode($t['0']);
	}
}
